/*
 *
 *  * AUTHOR：SIGHT
 *  *
 *  * DESCRIPTION：create the File, and add the content.
 *  *
 *  * Copyright © ZhiMore. All Rights Reserved
 *  *
 *
 */

package com.lx.commonlib.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.SystemClock;

import com.lx.commonlib.BuildConfig;

import java.util.List;
import java.util.Stack;
import java.util.logging.Logger;

/**
*  类说明：Activity管理类
*  created by liuxiong on 2017/11/14
**/
public class AppManager {

    private static List<Activity> activityStack;

    private AppManager() {
    }

    private static class LazyHolder {
        private static final AppManager INSTANCE = new AppManager();
    }

    /**
     * 单一实例
     */
    public static AppManager getInstance() {
        return LazyHolder.INSTANCE;
    }

    /**
    * 方法说明:  获取当前的Activity的数量
    * created by liuxiong on 2017/11/15 11:01
    */
    public int getActivityCount(){
        if(activityStack!=null){
            return activityStack.size();
        }else{
            return 0;
        }
    }

    /**
     * 获取指定类名的的Activity
     * @param claz
     * @return
     */
    public Activity getActivity(Class<? extends Activity> claz){
        if(activityStack==null||activityStack.size()==0){
            return null;
        }
        Activity activity01=null;
        for(Activity activity:activityStack){
            if(activity.getClass().equals(claz)){
                activity01=activity;
            }
        }
        return activity01;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取最后一个的activity
     */
    public Activity getLastActivity(){
        if(activityStack==null||activityStack.size()==0){
            return null;
        }
        return activityStack.get(activityStack.size()-1);
    }

    /**
     * 获取倒数第二个的activity
     */
    public Activity getLastSecondActivity(){
        if(activityStack==null||activityStack.size()<=1){
            return null;
        }
        return activityStack.get(activityStack.size()-2);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        Activity activity = getLastActivity();
        return activity;
    }

    /**
    * 方法说明:  从栈中移除Activity
    * created by liuxiong on 2018/1/8 10:34
    */
    public void removeActivity(Activity activity) {
        if(activityStack==null||activityStack.size()==0){
            return ;
        }
        if (activity != null&&activityStack.contains(activity)){
            activityStack.remove(activity);
        }
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = getLastActivity();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
        }
    }

    /**
     * 方法说明:  结束当前Activity（堆栈中最后一个压入的）
     * @param
     *@author liuxiong
     *@time 2017/7/27 15:22
     */
    public void finishCurrentActivity() {
        Activity activity = getLastActivity();
        finishActivity(activity);
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public void AppExit(boolean isCrash) {
        try {
            //finish所有activity
            finishAllActivity();
            if(BuildConfig.DEBUG&&isCrash){ //debug环境下睡眠10s
                SystemClock.sleep(10*1000);
            }
            //杀死主进程
            LogUtil.d("----杀死进程，退出应用");
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}